#include "fastaclustergenerator.h"

//initialize fasta file name
FastaClusterGenerator::FastaClusterGenerator(QString ff, QObject *parent) :
    QObject(parent)
{
    fastaFile.append(ff);
}


//start cdhit run
void FastaClusterGenerator::process()
{
    //----------------------------------------------
    //FIRST read fasta and get gene clusters
    //  Cluster output put into programFiles directory
    QString temp = fastaFile;
    int lastBackslash = temp.lastIndexOf("/");
    temp.remove(0, lastBackslash+1 );
    temp.replace(".", "_");
    QString outputClusterFile =  QApplication::applicationDirPath().append("/programFiles/dataFiles/").append(temp).append("_cdHitOut");//QApplication::applicationDirPath().append( "/programFiles/cdHitOut" );//QDir::currentPath().append("/cdHitOut");   //"C:\\Documents and Settings\\jaurisa\\My Documents\\Development\\sagevis\\sageVis-restructured\\SageVis\\data\\contigs\\cdHitOut2";
    readFastaOutputCdHitCluster( fastaFile, outputClusterFile );

    //-----------------------------------------------
    //THEN read gff file to create datasets
    //      Reformatted file put in programFiles directory
    QString inputClusterFile = outputClusterFile.append(".clstr");
    emit( clusterFileGeneratedSignal(inputClusterFile) );
    emit( finished() );
}


//This function sends a Fasta file to the cd-hit algorithm which clusters all the genes based on sequence similarity
bool FastaClusterGenerator::readFastaOutputCdHitCluster(QString inputFastaFile, QString outputClusterFile)
{
    qDebug() << "running cdhit QProcess on " << inputFastaFile << endl;

    //-----------SETUP PROGRAM--------------------
    QString program = QApplication::applicationDirPath().append("/programFiles/programs/cd-hit-win/cd_hit");//"C:\\Documents and Settings\\jaurisa\\My Documents\\Development\\sagevis\\sageVis-restructured\\SageVis\\programs\\cd-hit-win\\cd_hit";
#if defined(Q_OS_MAC)
    program =  QApplication::applicationDirPath().append("/programFiles/programs/cd-hit-mac/cd-hit");
//    qDebug() << "mac! " << program <<  " " << inputFastaFile << " " << outputClusterFile << endl;
#endif
    QStringList arguments;
    arguments << "-i" << inputFastaFile << "-o" << outputClusterFile <<
                 "-c" << ".9" << "-n" << "5" << "-d" << "1000";

    //-------------INITIATE QPROCESS----------------
    cdHit = new QProcess(this);
    cdHit->setProcessChannelMode(QProcess::MergedChannels);
    cdHit->start(program, arguments);
    qDebug() << "cdhit started " << endl;

    //emit( trackProgress(10) ); //NOTE- later can catch these track progress signals

    //--------------WAIT FOR THE START-------------
    if(!cdHit->waitForStarted())
    {
        qDebug() << "not waiting for started " << endl;
        return false; //note - not handling false return right now...
    }


    //emit( trackProgress(15) );

    //-------------RUN PROGRAM--------------------
    QByteArray data;//this holds the output of the file
    while(cdHit->waitForReadyRead())
    {
        data.append(cdHit->readAll());//turn this on to output results
        qDebug() << "... cd-hit running on file:  " << inputFastaFile << endl;
    }

    //-------------OUTPUT RESULTS------------------
    qDebug(data.data());
    qDebug("Done!");

    qDebug() << "Writing to " << outputClusterFile << endl;
    if( cdHit->error() )
    {
        qDebug() << "cdhit error" << endl;
        return false;
    }

    return cdHit->exitStatus() == QProcess::NormalExit;
}
